<?php
    namespace app\api\model;
    use app\common\model\Model;
    use think\Db;
    
//  model 类
    class LoginModel extends Model
    {
        const TABLE_NAME = 'user';
        const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

        protected $name = self::TABLE_NAME;

        //储存新用户
        public static function saveNewUser($phone,$openid,$user_name){
            $info = self::getUserByPhone($phone);
            if(empty($info)){

                $data =['user_phone' => $phone, 'wx_openid' => $openid];
                if(empty($user_name))
                {
                    $data['user_name']='hz_'.substr($phone,8);
                }else{
                    $data['user_name']=$user_name;
                }
                return self::commonQuery(self::TABLE_NAME)->insert($data,false,true);
            }else if(empty($info['wx_openid']))
            {
                self::commonQuery(self::TABLE_NAME)->where(['user_id'=>$info['user_id']])
                    ->setField('wx_openid',$openid);
            }
            return false;
        }
        //公众号 openid 
       public static function getUserInfoByWxOpenid($wx_openid)
       {
           $info =  self::commonQuery(self::TABLE_NAME)->where(['wx_openid'=>$wx_openid])->find();
           if(empty($info) || $info['is_del']==1)
           {
               return [];
           }
           return $info;

       }
        // 注册 用户 ： 密码、身份
        
        public static function addUserWithPasswdAndType($phone,$openid,$user_name,$avatar,$password,$type)
        {
            $info = self::getUserByPhone($phone);
            if(empty($info)){

                $data =['user_phone' => $phone, 'wx_openid' => $openid,
                     'user_passwd'=>$password,'user_type'=>$type,
                    'register_time'=>time()
                ];
                if(empty($user_name))
                {
                    $data['user_name']='hz_'.substr($phone,8);
                }else{
                    $data['user_name']=$user_name;
                }

                if(!empty($avatar))
                {
                    $data['user_avatar']=$avatar;
                }
                return self::commonQuery(self::TABLE_NAME)->insert($data,false,true);
            }else if(empty($info['wx_openid']))
            {
                self::commonQuery(self::TABLE_NAME)->where(['user_id'=>$info['user_id']])
                     ->setField('wx_openid',$openid);
            }
            return false;
        }

        //用户登录
        public static function  getUserByPhone($phone)
        {
            return  self::commonQuery(self::TABLE_NAME)
                ->where(['user_phone'=>$phone,'is_del'=>0])
                ->find();

        }
        public static function handleLoginUserInfo($info)
        {
            return ['user_id'=>$info['user_id'],
                'user_name'=>$info['user_name'],
                'user_avatar'=>$info['user_avatar'],
                'user_type'=>$info['user_type']
            ];
        }
        public static function updateUserPassword($user_id,$password)
        {
            return self::commonQuery(self::TABLE_NAME)
                   ->where(['user_id'=>$user_id])
                   ->setField('user_passwd',$password);
        }
    }


    